package domain;

import java.util.ArrayList;
import java.util.List;

public class Payment extends ModelObject {
	
	private String description;
	private double amount;
	private BalanceMonth balanceMonth;
	private List<Roommate> participants;
	private Roommate author;
	
	public Payment(){}
	
	public Payment(BalanceMonth balanceMonth, String description, double amount, List<Roommate> participants, Roommate author) {
		this.balanceMonth = balanceMonth;
		this.description = description;
		this.amount = amount;
		this.participants = participants;
		this.setAuthor(author);
	}
	
	public Payment(BalanceMonth balanceMonth, String description, double amount, Roommate author) {
		this.balanceMonth = balanceMonth;
		this.description = description;
		this.amount = amount;
		this.setAuthor(author);
	}
	
	public void addParticipant(Roommate roommate) {
		if (this.participants == null) {
			this.participants = new ArrayList<Roommate>();
		}
		this.participants.add(roommate);
	}
	
	public void removeParticipant(Roommate roommate) {
		if (this.participants == null) {
			return;
		}
		for (Roommate currentRoommate : participants) {
			if (currentRoommate.getId() == roommate.getId()) {
				participants.remove(currentRoommate);
				return;
			}
		}
	}
	
	/**
	 * @return the balanceMonth
	 */
	public BalanceMonth getBalanceMonth() {
		return balanceMonth;
	}
	
	/**
	 * @param balanceMonth the balanceMonth to set
	 */
	public void setBalanceMonth(BalanceMonth balanceMonth) {
		this.balanceMonth = balanceMonth;
	}
	
	/**
	 * @return the participants
	 */
	public List<Roommate> getParticipants() {
		List<Roommate> roommateReturn = new ArrayList<Roommate>();
		for (Roommate roommate : participants) {
			roommateReturn.add(roommate);
		}
		return roommateReturn;
	}
	
	/**
	 * @param participants the participants to set
	 */
	public void setParticipants(List<Roommate> participants) {
		if (this.participants == null) {
			this.participants = new ArrayList<Roommate>();
		}
		for (Roommate roommate : participants) {
			this.participants.add(roommate);
		}
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return the amount
	 */
	public double getAmount() {
		return amount;
	}

	/**
	 * @param amount the amount to set
	 */
	public void setAmount(double amount) {
		this.amount = amount;
	}

	/**
	 * @return the author
	 */
	public Roommate getAuthor() {
		return author;
	}

	/**
	 * @param author the author to set
	 */
	public void setAuthor(Roommate author) {
		this.author = author;
	}

	public Payment clone() {
		Payment newPayment = new Payment();
		newPayment.setId(getId());
		newPayment.setVersion(getVersion());
		newPayment.setAmount(getAmount());
		newPayment.setAuthor(getAuthor());
		newPayment.setBalanceMonth(getBalanceMonth());
		newPayment.setDescription(getDescription());
		newPayment.setParticipants(getParticipants());
		return newPayment;
	}
}
